package com.hankol.newretail.fragment

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseLazyFragment
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.newretail.R
import com.hankol.newretail.adapter.MallGoodsListOnelineOneAdapter
import com.hankol.newretail.adapter.MallGoodsListOnelineTwoAdapter
import com.hankol.newretail.bean.GoodListBean
import com.hankol.newretail.databinding.FragmentMallGoodListBinding
import com.hankol.newretail.viewmodel.HomeMallViewModel
import com.baselibrary.view.MarginDecoration
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/*新零售商城-商品分类下的列表*/
class MallGoodListFragment(var mType: String, var isOneToOne: Boolean = true) :
    BaseLazyFragment(false) {

    lateinit var binding: FragmentMallGoodListBinding
    private var viewModel: HomeMallViewModel? = null
    val goodListOnelineOneAdapter: MallGoodsListOnelineOneAdapter by lazy { MallGoodsListOnelineOneAdapter() }
    val goodListOnelineTwoAdapter: MallGoodsListOnelineTwoAdapter by lazy { MallGoodsListOnelineTwoAdapter() }
    var page = 1
    private var hasnext: Boolean = false
    private lateinit var mDataBean: GoodListBean

    companion object {

        fun newInstance(mType: String, isOneToOne: Boolean = true): MallGoodListFragment {
            return MallGoodListFragment(mType, isOneToOne)
        }
    }

    override fun init() {
        binding = getDataBinding<FragmentMallGoodListBinding>()
        viewModel = getFragmentViewModelProvider(this).get(HomeMallViewModel::class.java)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initView()
        initListener()
        initObserver()
        lazyLoadData()
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_mall_good_list
    }

    private fun initView() {
        binding.rvList.setLayoutManager(if (isOneToOne) LinearLayoutManager(activity) else GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false))
        binding.rvList.setAdapter(if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter)
        if (!isOneToOne) {
            binding.rvList.addItemDecoration(MarginDecoration(requireActivity(), 9))
        }
    }

    private fun initListener() {
        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }

        (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).setOnItemClickListener { adapter, view, position ->
            //进入商品详情
            val map = HashMap<String, Any>()
            map[Constants.GOODS_DETAIL_ID] = (adapter.data[position] as GoodListBean).id
            map[Constants.GOODS_DETAIL_SOURCE] = "商城首页"
            ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS,map)
        }
        (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).setOnItemChildClickListener { adapter, view, position ->
            val bean = (adapter.data[position] as GoodListBean)
            mDataBean = bean
            when (view.id) {
            }
        }
    }

    private fun initObserver() {
        viewModel?.goodsDataListLD?.observe(requireActivity(), Observer {
            binding.smartRefresh.finishRefresh()
            if (it == null || it.data.size == 0) {
                //无数据
                if (page != 1) {
                    binding.smartRefresh.finishLoadMore()
                }
                (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).emptyView =
                    EmptyViewUtil.getEmptyView(Constants.TYPE_GOOD_LIST, binding.rvList, "暂无商品")
            } else {
                if (it.data.checkNotNull()) {
                    hasnext =
                        it.totalPage != (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).data.size
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).setNewData(it.data)
                    } else {
                        (if (isOneToOne) goodListOnelineOneAdapter else goodListOnelineTwoAdapter).addData(it.data)
                    }
                    page++
                }
            }
        })
    }

    override fun lazyLoadData() {
        page = 1
        loadData()
    }

    fun loadData() {
        viewModel?.requestGoodClassifyList(mType, page)
    }


    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType.equals(MessageBus.msgId_refresh_home_goods_list)) {
            lazyLoadData()
        }
    }

    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }

}

